﻿/* 
 * Since 2008 - 2012
 */
package com.hh.bss.project.controller;

import static com.hh.bss.util.extjs.JsonHelper.outJson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import com.hh.bss.auth.client.OnlineManager;
import com.hh.bss.auth.client.OnlineUser;
import com.hh.bss.base.*;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.project.dao.*;
import com.hh.bss.project.entity.*;
import com.hh.bss.project.service.*;
import com.hh.bss.project.vo.query.*;
import com.hh.bss.util.*;
import com.hh.bss.util.extjs.ExtJsPageHelper;
import com.hh.bss.util.extjs.ListRange;

import java.util.*;




@Controller
public class SearchProjectCvController extends BaseSpringController {
	// 默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null;

	private SearchProjectCvService searchProjectCvService;

	private SearchProjectCv searchProjectCv;
	java.lang.Integer id = null;
	private String[] items;

	/** 通过spring自动注入 */
	public void setSearchProjectCvServiceImpl(SearchProjectCvService service) {
		this.searchProjectCvService = service;
	}

	public void setId(java.lang.Integer val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}

	/**
	 * ExtGrid使用 列表
	 * 
	 * @throws IOException
	 */
	public void list(HttpServletRequest request, HttpServletResponse response, SearchProjectCvQuery searchProjectCvQuery) throws IOException {
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		Page page = searchProjectCvService.findPage(searchProjectCvQuery);

		List<SearchProjectCv> SearchProjectCvlist = (List) page.getResult();
		ListRange<SearchProjectCv> resultList = new ListRange<SearchProjectCv>();
		resultList.setRows(SearchProjectCvlist);
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response, resultList);
	}

	/**
	 * ExtGrid使用 列表
	 * 
	 * @throws IOException
	 */
	public void projectcvlist(HttpServletRequest request, HttpServletResponse response, SearchProjectCvQuery searchProjectCvQuery) throws IOException {
		OnlineManager onlineManager = super.getOnlineManager(request, response);
		if (onlineManager != null) {
			OnlineUser onlineUser = onlineManager.getOnlineUser();
			searchProjectCvQuery.setOnlineUser(onlineUser);
		}
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		Page page = searchProjectCvService.findProjectCvPage(searchProjectCvQuery);

		List<SearchProjectCv> SearchProjectCvlist = (List) page.getResult();
		ListRange<SearchProjectCv> resultList = new ListRange<SearchProjectCv>();
		resultList.setRows(SearchProjectCvlist);
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response, resultList);
	}

	/**
	 * extGrid保存
	 * 
	 * @throws IOException
	 */
	public void save(HttpServletRequest request, HttpServletResponse response, SearchProjectCv searchProjectCv) throws IOException {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			// 设置创建用户和创建时间
			com.hh.bss.auth.client.OnlineManager onlineManager = super.getOnlineManager(request, response);
			if (onlineManager != null) {
				com.hh.bss.auth.client.OnlineUser onlineUser = onlineManager.getOnlineUser();
				if (onlineUser != null && com.hh.bss.util.BeanUtils.isExistsProperty(SearchProjectCv.class, "creator")) {
					try {
						com.hh.bss.util.BeanUtils.setProperty(searchProjectCv, "creator", onlineUser.getUsername());
					} catch (Exception ex) {
					}
				}
			}

			if (com.hh.bss.util.BeanUtils.isExistsProperty(SearchProjectCv.class, "createDt")) {
				try {
					com.hh.bss.util.BeanUtils.setProperty(searchProjectCv, "createDt", new Date());
				} catch (Exception ex) {
				}
			}

			searchProjectCvService.save(searchProjectCv);
			result.put("success", true);
			result.put("msg", "添加成功!");
		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response, result);
	}

	/**
	 * extGrid修改
	 * 
	 * @throws IOException
	 */
	public void update(HttpServletRequest request, HttpServletResponse response, SearchProjectCv searchProjectCv) throws IOException {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			SearchProjectCv oldsearchProjectCv = (SearchProjectCv) searchProjectCvService.getById(searchProjectCv.getId());

			bind(request, oldsearchProjectCv);
			// 设置修改用户和修改时间
			com.hh.bss.auth.client.OnlineManager onlineManager = super.getOnlineManager(request, response);
			if (onlineManager != null) {
				com.hh.bss.auth.client.OnlineUser onlineUser = onlineManager.getOnlineUser();
				if (onlineUser != null && com.hh.bss.util.BeanUtils.isExistsProperty(SearchProjectCv.class, "modifier")) {
					try {
						com.hh.bss.util.BeanUtils.setProperty(oldsearchProjectCv, "modifier", onlineUser.getUsername());
					} catch (Exception ex) {
					}
				}
			}

			if (com.hh.bss.util.BeanUtils.isExistsProperty(SearchProjectCv.class, "modifiedDt")) {
				try {
					com.hh.bss.util.BeanUtils.setProperty(oldsearchProjectCv, "modifiedDt", new Date());
				} catch (Exception ex) {
				}
			}

			searchProjectCvService.update(oldsearchProjectCv);
			result.put("success", true);
			result.put("msg", "修改成功!");
		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response, result);
	}

	public void cvtitleupdate(HttpServletRequest request, HttpServletResponse response, SearchProjectCvQuery searchProjectCvQuery) throws IOException {
		Map<String, Object> result = new HashMap<String, Object>();
		try {

			com.hh.bss.auth.client.OnlineManager onlineManager = super.getOnlineManager(request, response);
			if (onlineManager != null) {
				com.hh.bss.auth.client.OnlineUser onlineUser = onlineManager.getOnlineUser();
				if (onlineUser != null && com.hh.bss.util.BeanUtils.isExistsProperty(SearchProjectCv.class, "modifier")) {
					try {
						searchProjectCvQuery.setModifier(onlineUser.getUsername());
					} catch (Exception ex) {
					}
				}
			}
			searchProjectCvService.updateCvDeptTitle(searchProjectCvQuery);
			result.put("success", true);
			result.put("msg", "修改成功!");
		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response, result);
	}

	public void deletecv(HttpServletRequest request, HttpServletResponse response, SearchProjectCvQuery searchProjectCvQuery) throws IOException {
		Map<String, Object> result = new HashMap<String, Object>();
		try {

			searchProjectCvService.deleteCv(searchProjectCvQuery);
			result.put("success", true);
			result.put("msg", "删除成功!");
		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response, result);
	}

	public void deleteprojectcv(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, Object> result = new HashMap<String, Object>();

		String ids = request.getParameter("cvId");
		String project = request.getParameter("projectId");
		String[] idarray = ids.split(",");

		try {
			SearchProjectCvQuery searchProjectCvQuery = null;
			for (int i = 0; i < idarray.length; i++) {
				java.lang.Integer id = new java.lang.Integer((String) idarray[i]);
				// searchProjectCvService.removeById(id);
				searchProjectCvQuery = new SearchProjectCvQuery();

				searchProjectCvQuery.setCvId(id);
				searchProjectCvQuery.setProjectId(StringHelper.parseInteger(project, 0));
				searchProjectCvService.deleteProjectCv(searchProjectCvQuery);
			}
			result.put("success", true);
			result.put("msg", "删除成功");
		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}

		outJson(response, result);
	}

	/**
	 * extGrid删除
	 * 
	 * @throws IOException
	 */
	public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String ids = request.getParameter("ids");
		String[] idarray = ids.split(",");
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			for (int i = 0; i < idarray.length; i++) {
				java.lang.Integer id = new java.lang.Integer((String) idarray[i]);
				searchProjectCvService.removeById(id);
			}
			result.put("success", true);
			result.put("msg", "删除成功");
		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response, result);
	}

	public void cvJoinPorjectCompany(HttpServletRequest request, HttpServletResponse response, SearchProjectCvQuery searchProjectCvQuery) {
		// String cv_ids = request.getParameter("cv_ids");
		// String project_id = request.getParameter("project_id");
		// String[] idarray = cv_ids.split(",");
		// List<SearchProjectCv> projectCvs = new ArrayList<SearchProjectCv>();
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			// for(int i=0;i<idarray.length;i++){
			// SearchProjectCv projectCv = new SearchProjectCv();
			// projectCv.setCvId(Integer.valueOf(idarray[i]));
			// projectCv.setProjectId(Integer.valueOf(project_id));
			// projectCv.setCreator("admin");
			// projectCv.setCreateDt(new Date());
			// projectCvs.add(projectCv);
			// }
			// searchProjectCvService.cvJoinProject(projectCvs);

			result.put("success", true);
			result.put("msg", "添加成功!");
		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response, result);

	}

	@SuppressWarnings("unchecked")
	public void cvJoinPorject(HttpServletRequest request, HttpServletResponse response) {
		String cv_ids = request.getParameter("cv_ids");
		String project_ids = request.getParameter("project_ids");
		String[] idarray = cv_ids.split(",");
		String[] projectIdArr = project_ids.split(",");
		List<SearchProjectCv> projectCvs = new ArrayList<SearchProjectCv>();

		com.hh.bss.auth.client.OnlineManager onlineManager = super.getOnlineManager(request, response);
		com.hh.bss.auth.client.OnlineUser onlineUser = null;
		if (onlineManager != null) {
			onlineUser = onlineManager.getOnlineUser();

		}
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			// 循环项目
			for (String projectId : projectIdArr) {
				for (String cvId : idarray) {
					if (searchProjectCvService.checkCvIntegrity(Integer.valueOf(cvId))) {
						SearchProjectCv projectCv = new SearchProjectCv();
						projectCv.setProjectId(Integer.valueOf(projectId));
						projectCv.setCvId(Integer.valueOf(cvId));
						if (onlineUser != null)
							projectCv.setCreator(onlineUser.getUsername());
						projectCv.setCreateDt(new Date());
						projectCv.setCompanyId(0);
						projectCv.setTitleId(0);
						projectCv.setDeptId(0);
						projectCvs.add(projectCv);
					} else {
						result.put("failure", true);
						result.put("msg", "添加失败! 人选:" + cvId + " ,简历完整度 为 ('申请删除 或者 SC要求删除') 状态 不能添加到Search List !");
						outJson(response, result);
						return;
					}

				}
			}
			searchProjectCvService.cvJoinProject(projectCvs);
			result.put("success", true);
			result.put("msg", "添加成功!");
		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response, result);

	}

}
